﻿using System;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Revit;
using YArchitech.Temp;

namespace YArchitech.Plumbing
{
	internal static class YJKElementNodeExtern
	{
		public static HYPlumbingObject CreateObject(this ElementNode en, Autodesk.Revit.DB.Document doc)
		{
			Element element = en.Element;
			if (DbUtils.IsPipe(element))
			{
				return HYRevitExchange.CreatePipe(element as Pipe);
			}
			if (DbUtils.IsFlexPipe(element))
			{
				return HYRevitExchange.CreatePipe(element as FlexPipe);
			}
			FamilyInstance fi = element as FamilyInstance;
			if (DbUtils.IsPipeFitting(element))
			{
				return HYRevitExchange.CreateFitting(fi);
			}
			if (DbUtils.IsPipeAccessory(element))
			{
				return HYRevitExchange.CreatePipeValve(fi);
			}
			if (DbUtils.IsPlumbingFixtures(element))
			{
				return HYRevitExchange.CreateBathroom(fi, doc);
			}
			if (DbUtils.IsFireEquipment(element))
			{
				return HYRevitExchange.CreateFire(fi);
			}
			if (DbUtils.IsSprinkler(element))
			{
				return HYRevitExchange.CreateSprinkler(fi);
			}
			return null;
		}

		public static HYPlumbingObject CreateObject(this ElementNode en, ref HYSystemCal system)
		{
			if (en.Element == null)
			{
				return null;
			}
			HYPlumbingObject hyplumbingObject = en.CreateObject(system.doc);
			if (hyplumbingObject == null)
			{
				return null;
			}
			system.Add(hyplumbingObject);
			ElementNode next = en.Next;
			if (next != null)
			{
				hyplumbingObject.AddNext(next, ref system);
			}
			if (hyplumbingObject is HYPipeValveCal)
			{
				if (en.SubElementNode != null && en.SubElementNode.Count > 0)
				{
					hyplumbingObject.AddNext(en.SubElementNode.First<ElementNode>(), ref system);
				}
			}
			else if (en.SubElementNode != null)
			{
				foreach (ElementNode ndSub in en.SubElementNode)
				{
					en.AddSubObject(hyplumbingObject, ndSub, ref system);
				}
			}
			return hyplumbingObject;
		}

		private static void AddNext(this HYPlumbingObject objThis, ElementNode enNext, ref HYSystemCal system)
		{
			HYPlumbingObject hyplumbingObject = enNext.CreateObject(ref system);
			if (hyplumbingObject != null)
			{
				objThis.AddNext(hyplumbingObject);
			}
		}

		public static void AddSubObject(this ElementNode enThis, HYPlumbingObject objThis, ElementNode ndSub, ref HYSystemCal system)
		{
			if (ndSub == null)
			{
				return;
			}
			HYPlumbingObject hyplumbingObject = ndSub.CreateObject(ref system);
			if (hyplumbingObject == null)
			{
				return;
			}
			objThis.Flow += hyplumbingObject.Flow;
			if (objThis is HYPipeCal)
			{
				objThis.AddNext(hyplumbingObject);
				return;
			}
			if (objThis is HYFittingCal)
			{
				HYFittingCal hyfittingCal = objThis as HYFittingCal;
				if (hyfittingCal.Type == HYFittingCal.eType.Tee || hyfittingCal.Type == HYFittingCal.eType.Cross)
				{
					hyfittingCal.AddNext(hyplumbingObject);
				}
			}
		}
	}
}
